package com.ld.customer.view;

import com.ld.customer.dao.CustomerDao1;
import com.ld.customer.entity.Customer;
import com.ld.customer.utility.CMUTility;
import java.util.List;

public class CustomerView {

    private CustomerDao1 cd = CustomerDao1.getCd();

    public void enterMainMenu() {
        do {
            System.out.println("\n-----------------欢迎来到客户管理系统主页面------------------\n");
            System.out.println("\t\t 1,登录1\n");
            System.out.println("\t\t 2,退出2\n");
            System.out.print("\t\t 请输入您的选择:");
            char key = CMUTility.readMenuSelection();
            switch (key) {
                case '1':
                    System.out.print("请输入用户名:");
                    String username = CMUTility.readString(10);
                    System.out.print("请输入密码:");
                    String password = CMUTility.readString(10);
                    if ("LSiakoh".equals(username) && "123456".equals(password)) {
                        enterMenu();
                    }
                    break;
                case '2':
                    System.out.println("\t您确定要退出吗？如果确定请输入y,否则请输入n");
                    System.out.print("\t\t 请输入您的选择:");
                    char yn = CMUTility.readConfirmSelection();
                    if ('Y' == yn) {
                        System.exit(0);
                    }
                    break;
            }
        } while (true);
    }


    public void enterMenu() {
        boolean flag = true;
        do {
            System.out.println("\n-----------------欢迎来到客户管理系统------------------\n");
            System.out.println("\t\t 1,查看所有客户请选择1\n");
            System.out.println("\t\t 2,删除客户请选择2\n");
            System.out.println("\t\t 3,更新客户请选择3\n");
            System.out.println("\t\t 4,添加新客户请选择4\n");
            System.out.println("\t\t 5,退出请选择5\n");
            System.out.print("\t\t 请输入您的选择:");
            char key = CMUTility.readMenuSelection();
            switch (key) {
                case '1':
                    getAllCustomer();
                    break;
                case '2':
                    deleteCutomer();
                    break;
                case '3':
                    updateCustomer();
                    break;
                case '4':
                    addNewCustomer();
                    break;
                case '5':
                    System.out.println("\t您确定要退出吗？如果确定请输入y,否则请输入n");
                    System.out.print("\t\t 请输入您的选择:");
                    char yn = CMUTility.readConfirmSelection();
                    if ('Y' == yn) {
                        flag = false;
                    }
                    break;
            }
        } while (flag);
    }

    private void addNewCustomer() {
        System.out.println("\n----------------------添加新客户----------------------\n");
        System.out.println("请输入id");
        int id = CMUTility.readInt();
        System.out.print("请输入姓名:");
        String name = CMUTility.readString(10);
        System.out.print("请输入性别:");
        String gender = CMUTility.readString(5);
        System.out.print("请输入年龄:");
        int age = CMUTility.readInt();
        System.out.print("请输入电话:");
        String phone = CMUTility.readString(15);
        System.out.print("请输入邮箱:");
        String email = CMUTility.readString(15);
//       Customer cu = new Customer(id,name, gender, age, phone, email);
        if (cd.update(cd.getInsertSql(), id,name, gender, age, phone, email)  ) {
            System.out.println("\n----------------------添加成功----------------------\n");
        } else {
            System.out.println("\n----------------------添加失败----------------------\n");
        }
    }

    /**
     * 更新客户
     */
    private void updateCustomer() {
        System.out.println("\n----------------------更新客户----------------------\n");
        int id = 0;
        Customer cu = null;
        System.out.print("\n如果退出更新请输入(-1),请输入您的选择更新客户的id:\n");
        for (;;) {
            id = CMUTility.readInt();
            if (id == -1)
                return;

            cu = cd.selectById(id);
            if (cu == null) {
                System.out.println("\n----------------------没找到指定客户----------------------\n");
                continue;
            }

            System.out.println("\t您确定要退出更新吗？如果确定请输入y,否则请输入n");
            System.out.print("\t\t 请输入您的选择:");
            char yn = CMUTility.readConfirmSelection();
            if ('N' == yn) {
                return;
            }

            System.out.print("姓名" + cu.getName() + "请输入需要更新的姓名:");
            String name = CMUTility.readString(10, cu.getName());
            System.out.print("性别" + cu.getGender() + "请输入需要更新的性别:");
            String gender = CMUTility.readString(5, cu.getGender());
            System.out.print("年龄" + cu.getAge() + "请输入需要更新的年龄:");
            int age = CMUTility.readInt(cu.getAge());
            System.out.print("电话" + cu.getPhone() + "请输入需要更新的电话:");
            String phone = CMUTility.readString(15, cu.getPhone());
            System.out.print("邮箱" + cu.getEmail() + "请输入需要更新的邮箱:");
            String email = CMUTility.readString(15, cu.getEmail());
//           cu = new Customer(name, gender, age, phone, email);
            if (cd.update( cd.getUpdateSql(), name , gender , age , phone , email , id)) {
                System.out.println("\n----------------------修改成功----------------------\n");
            } else {
                System.out.println("\n----------------------修改失败----------------------\n");
            }
        }

    }


    private void deleteCutomer() {
        System.out.println("\n----------------------删除客户----------------------\n");
        int id = 0;
        Customer cu = null;
        System.out.print("\n如果退出删除请输入(-1),请输入您的选择删除客户的id:\n");
        for (;;) {
            id = CMUTility.readInt();
            if (id == -1)
                return;

            cu = cd.selectById(id);
//          cu = cd.getConnection(Customer.class , cd.getId() , id);
            if (cu == null) {
                System.out.println("\n----------------------没找到指定客户----------------------\n");
            } else
                break;
        }

        System.out.println("\t您确定要删除吗？如果确定请输入y,否则请输入n");
        System.out.print("\t\t 请输入您的选择:");
        char yn = CMUTility.readConfirmSelection();
        if ('Y' == yn) {
            if (cd.update(cd.getDeleteSql() ,id)) {
                System.out.println("\n----------------------删除成功----------------------\n");
            } else {
                System.out.println("\n----------------------删除失败----------------------\n");
            }
        }
    }

    /**
     * 获取所有客户
     */
    private void getAllCustomer() {
        System.out.println("\n--------------------列出所有客户---------------------\n");
        List<Customer> ls = cd.getConnection(Customer.class , cd.getSelectAllSql());
        if (ls.size() == 0) {
            System.out.println("没有指定客户！！");
        } else {
            System.out.println("\tid\t姓名\t\t性别\t年龄\t电话\t邮箱\n");
            for (Customer c : ls) {
                System.out.println("\t" + c.getId() + "\t" + c.getName() + "\t" + c.getGender() + "\t" + " "
                        + c.getAge() + "\t" + c.getPhone() + "\t" + c.getEmail());
            }
        }
        System.out.println("\n--------------------列出客户完成---------------------\n");
    }

    public static void main(String[] args) {
        CustomerView cu = new CustomerView();
        cu.enterMainMenu();
    }
}
